package dk.dashboardFP.elements.button {
	import dk.dashboardFP.interactionElementFramwork.inputElement.CheckBox;
	import dk.dashboardFP.interactionElementFramwork.inputElement.CheckBoxEvent;
	import dk.dashboardFP.interactionElementFramwork.inputElement.ToggleButton;
	import dk.dashboardFP.modelDataHandler.properties.BooleanProperty;
	import dk.dashboardFP.modelDataHandler.properties.PropertyEvent;

	/**
	 * @author josef
	 */
	public class ToggleButtonPropertyElement extends ToggleButtonElement {
		private var _oBooleanProperty : BooleanProperty;
		private var _oCB : CheckBox;

		public function ToggleButtonPropertyElement(oBooleanProperty : BooleanProperty, sLabel : String, sPreLabel : String = "") {
			_oBooleanProperty = oBooleanProperty;
			super({}, sLabel, sPreLabel);
		}

		override protected function setupElements() : void {
			super.setupElements();
			
			_oCB=ToggleButton(_oButton);
			ToggleButton(_oButton).bAutoSetValue = false;
			
			//:::::::
			// register check box 
			_oCB.bAutoSetValue = false;
			_oCB.addEventListener(CheckBoxEvent.REQUEST_VALUE_CHANGE, onRequestValueChange);
			_oCB.set_value(_oBooleanProperty.bValue, false);
			
			//:::::::
			// register property 
			_oBooleanProperty.addEventListener(PropertyEvent.VALUE_CHANGE, onPropValueChange, false, 0, true);
		}

		private function onPropValueChange(event : PropertyEvent) : void {
			_oCB.set_value(_oBooleanProperty.bValue, false);
		}

		private function onRequestValueChange(event : CheckBoxEvent) : void {
			if (_oBooleanProperty.bValue) {
				_oBooleanProperty.set_bValue(false);
			} else {
				_oBooleanProperty.set_bValue(true);
			}
		}
	}
}
